Right Shift(>>)
It is a binary operator that takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, right-shifting an integer “a” with an integer “b” denoted as ‘(a>>b)‘ is equivalent to dividing a with 2^b.
Syntax:
a >> b;
- a: First Operand
- b: Second Operand
Example: let’s take a=5; which is 101 in Binary Form. Now, if “a is right-shifted by 2” i.e a=a>>2 then a will become a=a/(2^2). Thus, a=a/(2^2)=1 which can be written as 01.
C
// C Program to demonstrate // use of right-shift operator #include <stdio.h> // Driver code int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00000010 printf ( "a>>1 = %d\n" , (a >> 1)); // The result is 00000100 printf ( "b>>1 = %d" , (b >> 1)); return 0; } |
C++
// C++ Program to demonstrate // use of right-shift operator #include <iostream> using namespace std; // Driver code int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00000010 cout << "a>>1 = " << (a >> 1) << endl; // The result is 00000100 cout << "b>>1 = " << (b >> 1) << endl; return 0; } |
Output
a>>1 = 2 b>>1 = 4
Contact Us